using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

public class ChessDetail
{
    //todo:与UI结合,逻辑结合判断
    public bool CanMove;
    public bool CanUse;
    public bool IsShields;

    private bool propheticMark;//天命先知的标记
    private bool griefMark;//哀伤印记的标记
    private int chantBuff;//圣歌咏者的buff
    private bool emberVanguard;//烬灭先锋的buff
    public ChessType ChessType;
    public Vector2Int Position;
    private int totalHp;
    private int originalHp;
    public int Health;
    private int extraHp;
    public int TotalHp
    {
        get => totalHp;
        set {
            totalHp = value;
            //todo:事件更新UI
        }
    }
    
    public int OriginalHp
    {
        get => originalHp;
        set {
            originalHp = value;
            TotalHp = value+ExtraHp;
        }
    }

    public int ExtraHp
    {
        get => extraHp;
        set {
            extraHp = value;
            TotalHp = value+OriginalHp;
        }
    }

    private int attack;
    public int Attack
    {
        get => attack;
        set
        {
            attack = value;
            //todo:更新UI
        } 
    }

    public bool PropheticMark
    {
        get => propheticMark;
        set {
            propheticMark = value;
            //更新UI
        }
    }

    public int ChantBuff
    {
        get => chantBuff;
        set {
            chantBuff = value;
            //更新UI
        }
    }

    public bool GriefMark
    {
        get => griefMark;
        set {
            griefMark = value;
            //更新UI
        }
    }
    public bool EmberVanguard
    {
        get => emberVanguard;
        set {
            emberVanguard = value;
            //更新UI
        }
    }

    

    public ChessSide Side;
    public ChessDetail(Vector2Int position, int originalHp, int attack, ChessSide side,ChessType chessType)
    {
        Position = position;
        OriginalHp = originalHp;
        Health = originalHp;
        Attack = attack;
        Side = side;
        ChessType = chessType;
        CanMove = true;
        CanUse = true;
        IsShields = false;
    }
}

public class Chess : MonoBehaviour
{
    public Sprite player1BgSprite;
    public Sprite player2BgSprite;
    public SpriteRenderer bgSpriteRenderer;
    public SpriteRenderer chessSpriteRenderer;
    public BaseChessDateSo chessDate;
    
    public ChessDetail ChessDetail;

    public void InitChess(BaseChessDateSo cardDate,Vector2Int vector2Int,ChessSide side = ChessSide.Player1)
    {
        chessDate = cardDate;
        chessSpriteRenderer.sprite = chessDate.chessSprite;
        if (side == ChessSide.Player1)
        {
            bgSpriteRenderer.sprite = player1BgSprite;
        }else if (side == ChessSide.Player2)
        {
            bgSpriteRenderer.sprite = player2BgSprite;
        }
        ChessDetail = new ChessDetail(vector2Int,cardDate.health,cardDate.attack,side,cardDate.chessType);
        
    }

    private void OnDestroy()
    {
        if (chessDate)
        {
            foreach (var skill in chessDate.skill)
            {
                skill.Remove(this);
            }
        }
    }

    public ChessSide GetChessSide()
    {
        return ChessDetail.Side;
    }

    public bool Damage(int damage,Chess attackChess = null)
    {
        print(chessDate.chessName+"的总血量"+ChessDetail.TotalHp +"的额外血量" +ChessDetail.ExtraHp+"的正常血量"+ ChessDetail.OriginalHp);
        if (ChessDetail.TotalHp > damage)
        {
            if (attackChess)
            {
                EventCenter.GetInstance().EventTrigger<GameEvent>(SkillEventName.EventAttackChess.ToString(),new AttackChessEvent(attackChess,this));
            }

            if (ChessDetail.ExtraHp >= damage)
            {
                ChessDetail.ExtraHp -= damage;
            }
            else
            {
                damage -= ChessDetail.ExtraHp;
                ChessDetail.ExtraHp = 0;
                
                ChessDetail.OriginalHp -= damage;
            }
        }
        else
        {
            ChessDetail.TotalHp = 0;
            EventCenter.GetInstance().EventTrigger<GameEvent>(SkillEventName.EventKillChess.ToString(),new KillChessEvent(attackChess,this));
            EventCenter.GetInstance().EventTrigger<Vector2Int>("DisCardChess",ChessDetail.Position);
            print("被打死的"+chessDate.chessName+"的总血量"+ChessDetail.TotalHp +"的额外血量" +ChessDetail.ExtraHp+"的正常血量"+ ChessDetail.OriginalHp);
            return true;
        }
        print("被攻击的"+chessDate.chessName+"的总血量"+ChessDetail.TotalHp +"的额外血量" +ChessDetail.ExtraHp+"的正常血量"+ ChessDetail.OriginalHp);
        return false;
    }

    public void HealOriginHp(int heal)
    {
        if (chessDate.health >= ChessDetail.OriginalHp + heal)
        {
            ChessDetail.OriginalHp = chessDate.health;
        }
        else
        {
            ChessDetail.OriginalHp += heal;
        }
    }
    
    public void HealExtraHp(int heal)
    {

        ChessDetail.ExtraHp += heal;
    }

    public bool BeAttack(int damage,Chess attackChess = null)
    {
        if (ChessDetail.PropheticMark) damage++;
        if (ChessDetail.EmberVanguard) damage+=2;

        if (ChessDetail.IsShields)
        {
            ChessDetail.IsShields = false;
            return false;
        }
        return Damage(damage,attackChess);
    }

    public void Buff(int count)
    {
        if (ChessDetail.Attack + count <= 0)
        {
            ChessDetail.Attack = 0;
            return;
        }
        ChessDetail.Attack += count;
    }

    public void DispelsBuffs()
    {
        ChessDetail.Attack = chessDate.attack;
    }
    
    public void SetChessCanMove(bool canMove)
    {
        ChessDetail.CanMove = canMove;
    }
    
    public void SetChessCanUse(bool canUse)
    {
        ChessDetail.CanUse = canUse;
    }

    public bool GetCanMove()
    {
        return ChessDetail.CanMove;
    }

    public void SetChessShields(bool isShields)
    {
        ChessDetail.IsShields = isShields;
    }

    [ContextMenu(nameof(DebugChessDetail))]
    public void DebugChessDetail()
    {
        print("棋子的名字" + chessDate.chessName +
              "\n棋子的位置" + ChessDetail.Position +
              "\n棋子的攻击力" + ChessDetail.Attack +
              "\n棋子的初始生命值" + ChessDetail.Health +
              "\n棋子的原始生命值" + ChessDetail.OriginalHp +
              "\n棋子的额外生命值" + ChessDetail.ExtraHp +
              "\n棋子的总生命值" + ChessDetail.TotalHp +
              "\n棋子是否有护盾能够抵挡一次攻击" + ChessDetail.IsShields +
              "\n棋子是否能够移动" + ChessDetail.CanMove +
              "\n棋子是否能够使用" + ChessDetail.CanUse +
              "\n棋子是否有哀伤印记的标记" + ChessDetail.GriefMark +
              "\n棋子是否有天命先知的标记" + ChessDetail.PropheticMark +
              "\n棋子是否有圣歌咏者的buff" + ChessDetail.ChantBuff +
              "\n棋子是否有烬灭先锋的buff" + ChessDetail.EmberVanguard);
    }
    
    [ContextMenu(nameof(ChangeChessSide))]
    public void ChangeChessSide()
    {
        ChessDetail.Side = ChessDetail.Side == ChessSide.Player1 ? ChessSide.Player2 : ChessSide.Player1;
    }
}
